# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Sum, Ref
from ..base import OpsFeature

from .close import Close

class PSY(OpsFeature):
    """Technical Indicator - PSY

    - PSY = COUNT(CLOSE > REF(CLOSE, 1), N) / N * 100
    """
    def __init__(self, feature=None, N=12):
        self.feature = feature or Close()
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N != 12:
            name += '_'+str(self.N)
        return name

    def _load_internal(self, instrument, start_time, end_time, freq):
        psy = Sum(self.feature > Ref(self.feature, 1), self.N) / self.N * 100
        return psy.load(instrument, start_time, end_time, freq)
